package com.inossem.warehousestorage_inossem.adapter.inStorage.chuangyou;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.inossem.utils.base.Utils;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.in_storage.chuangyou.CYInStorageSerialNumberActivity;
import com.inossem.warehousestorage_inossem.bean.inStorage.chuangyou.CYInStorageProductionOverviewBean;

import java.util.ArrayList;
import java.util.List;

/**
 * TODO
 *
 * @author 王斯宇
 * @time on 2018/3/22 15:47
 * @email siyu.wang@inossem.com
 */
public class CYInStorageProductionCompleteMaterielDetailAdapter extends BaseAdapter {

    private Context mContext;

    private LayoutInflater inflater;

//    private CYInStorageProductionOverviewBean.OverViewBean.ItemListBean data;
    private List<String> list = new ArrayList<>();

    private String mCondition , mMaterialCode , mMaterielDescription , mLocation , mProductionNumber , mPackagingNumber , mUnit;

//    private CYInStorageProductionCompleteMaterielDetailActivity.ChangeStorageMaterielListener mListener;

    public CYInStorageProductionCompleteMaterielDetailAdapter(Context context , String condition , CYInStorageProductionOverviewBean.OverViewBean.ItemListBean data){
        this.mContext = context;
        inflater = LayoutInflater.from(context);
        this.mCondition = condition;
//        this.mListener = listener;
        this.mMaterialCode = data.getMat_code();
        this.mMaterielDescription = data.getMat_name();
        this.mLocation = data.getLocation_code() + "-" + data.getLocation_name();
        this.mProductionNumber = data.getQty();
        this.mPackagingNumber = data.getInStorageNumber() == null ? "0" : data.getInStorageNumber();
        this.mUnit = data.getName_zh();


    }

    @Override
    public int getCount() {
//        return list.size() + 1;
        return 4;
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        if (position == 0) {
            convertView = inflater.inflate(R.layout.cy_view_in_storage_package_code_first_item, null);
            EditText condition = (EditText) convertView.findViewById(R.id.code);
            ImageView search = (ImageView) convertView.findViewById(R.id.search);
            condition.setHint(R.string.activity_change_storage_materiel_list_please_input_material_information);
            condition.setText(mCondition);
            TextView materialCode = (TextView) convertView.findViewById(R.id.materielCode);
            TextView materielDescription = (TextView) convertView.findViewById(R.id.materielDescription);
            TextView location = (TextView) convertView.findViewById(R.id.location);
            TextView productionNumber = (TextView) convertView.findViewById(R.id.productionNumber);
            TextView packagingNumber = (TextView) convertView.findViewById(R.id.packagingNumber);
            materialCode.setText(mMaterialCode);
            materielDescription.setText(mMaterielDescription);
            location.setText(mLocation);
            productionNumber.setText(mProductionNumber + mUnit);
            packagingNumber.setText(mPackagingNumber + mUnit);
//            receiveMaterial.setOnClickListener(new NoDoubleClickListener() {
//                @Override
//                public void onNoDoubleClick(View v) {
//                    mListener.searchMaterial();
//                }
//            });

//            condition.addTextChangedListener(new QRCodeEditTextWatcher(condition) {
//                @Override
//                public void onScan(String text) {
//                    Utils.closeSoftKeyboard(mContext, condition);
//
//                }
//            });
            search.setOnClickListener(new NoDoubleClickListener() {
                @Override
                public void onNoDoubleClick(View v) {
                    Utils.closeSoftKeyboard(mContext, v);
                }
            });
        } else {
//            ChangeStorageMaterielListBean.MaterielListBean materielItem = mChangeStorageMaterielList.get(position - 1);
            ViewHolder viewHolder;
            if (convertView == null || (convertView != null && convertView.getTag() == null)) {
                convertView = inflater.inflate(R.layout.cy_view_change_storage_package_code, null);
                viewHolder = new ViewHolder();
                //初始化控件
                initView(viewHolder, convertView);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            //初始化数据
            initData(mContext, viewHolder, convertView , position);
        }
        return convertView;
    }
    private void initView(ViewHolder viewHolder, View view) {
        viewHolder.name = (TextView) view.findViewById(R.id.name);
        viewHolder.code = (TextView) view.findViewById(R.id.code);
        viewHolder.imageView = (ImageView) view.findViewById(R.id.image);

    }

    private void initData(Context context,ViewHolder viewHolder, View view , int position) {
        if (position < 2) {
            viewHolder.name.setText("打包码");
            viewHolder.code.setText("D00A120LC3");
            viewHolder.code.setTextColor(mContext.getResources().getColor(R.color.activity_receive_delivery_overview_font_color));
            view.setOnClickListener(new NoDoubleClickListener() {
                @Override
                public void onNoDoubleClick(View v) {
                    mContext.startActivity(new Intent(context , CYInStorageSerialNumberActivity.class));
                }
            });
        }else {
            viewHolder.name.setText("序列号");
            viewHolder.code.setText("1712 A1 0112 0001");
            viewHolder.code.setTextColor(mContext.getResources().getColor(R.color.common_font_item_name));
        }

        viewHolder.imageView.setBackgroundResource(R.drawable.ico_arrow3_down);

        view.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
//                mChangeStorageMaterielListener.deleteMaterielItem(materielItem.getMat_id() ,materielItem.getLocation_id() , materielItem.getStock_position_id() , materielItem.getBatch(), materielItem.getMat_code());
                return true;
            }
        });

    }
    public class ViewHolder {
        private TextView name , code ;
        private ImageView imageView;
    }
}
